<template>
  <div>
    <slot></slot>
    <div class="main-register">
      <van-form>
        <van-field
          v-model="username"
          name="账号"
          label="*账号"
          placeholder="账号"
          :rules="[{ required: true, message: '请填写账号' }]"
        />
        <van-field
          v-model="nickname"
          type="text"
          name="昵称"
          label="*昵称"
          placeholder="昵称"
          :rules="[{ required: true, message: '请填写昵称' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="*密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field
          v-model="passwordAngin"
          name="确认密码"
          label="*确认密码"
          placeholder="确认密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field
          v-model="phone"
          name="手机号"
          label="*手机号"
          placeholder="手机号"
          :rules="[{ required: true, message: '请填写手机号' }]"
        />
        <van-field
          v-model="code"
          name="验证码"
          label="*验证码"
          placeholder="验证码"
          :rules="[{ required: true, message: '请填写验证码' }]"
        />
        <!-- <button class="getcode" @click="getcode" v-html="time" :disabled="disabled">获取验证码</button> -->

        <a-button
          class="getcode"
          @click="getcode"
          v-html="time"
          :disabled="disabled"
          >获取验证码</a-button
        >
        <van-field
          v-model="sex"
          name="性别"
          label="性别"
          placeholder="性别"
          :rules="[{ required: false }]"
        />
        <van-field
          v-model="usermsg"
          name="个人简介"
          label="个人简介"
          placeholder="个人简介"
          :rules="[{ required: false }]"
        />
        <div style="margin: 16px">
          <van-button
            round
            block
            type="info"
            style="background-color: black; color: white"
            @click="submit"
            >注册</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { getcode, register } from "@/api/api";

export default {
  name: "Register",
  data() {
    return {
      username: "",
      password: "",
      nickname: "",
      phone: "",
      passwordAngin: "",
      code: "",
      sex: "",
      usermsg: "",
      //验证码
      disabled: false,
      time: "获取验证码",
      minute: 60,
      timer: null,
    };
  },
  methods: {
    getcode() {
      this.timer = setInterval(() => {
        this.disabled = true;
        this.minute--;
        this.time = `${this.minute}s`;
        if (this.minute == 0) {
          this.disabled = false;
          this.time = "获取验证码";
          this.minute = 60;
          clearInterval(this.timer);
        }
      }, 1000);
      getcode(this.phone).then((res) => {
        if (res.code == 0) {
          this.$message.success("发送成功，请稍等。。。。");
        }
      });
    },
    submit() {
      register(
        this.username,
        this.code,
        this.phone,
        this.password,
        this.nickname,
        this.sex,
        this.usermsg
      )
        .then((res) => {
          console.log(res);
          if (res.code == 0) {
            this.$message.success("注册成功");
            this.$router.push({ path: "/login" });
          } else {
            this.$message.error(`${res.data.msg}`);
          }
        })
        .catch(() => {
          this.$message.error("网络繁忙，请稍后再试！");
        });
    },
  },
};
</script>

<style scoped lang="less">
.back {
  font-size: 30px;
  margin: 15px;
}
.main-register {
  //position: relative;
  .getcode {
    float: left;
    position: relative;
    top: -30px;
    left: 70%;
    width: 27%;
  }
}
</style>